package com.chinairi.mall.controller;


import com.chinairi.common.utils.ResponseUtil;
import com.chinairi.mall.controller.viewobject.MaterialVO;
import com.chinairi.mall.model.Material;
import com.chinairi.mall.service.IMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.chinairi.common.controller.BaseController;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 物料表 前端控制器。
 * </p>
 *
 * @author gsl
 * @since 2020-08-26
 */
@RestController
@RequestMapping("/material")
public class MaterialController extends BaseController {

    @Autowired
    private IMaterialService iMaterialService;

    /**
     * 新增单个物料
     *
     * @param materialVO
     * @throws IOException
     */
    @PostMapping("/add")
    public void addMaterial(@RequestBody MaterialVO materialVO) throws IOException {
        iMaterialService.addMaterial(materialVO);
        ResponseUtil.showMessage("success", "新增物料成功", "");
    }

    /**
     * 查询物料
     * 根据物料名称、分类编码、物料编码
     *
     * @param materialVO
     * @throws IOException
     */
    @GetMapping("/select")
    public void selectMaterial(MaterialVO materialVO) throws IOException {
        List<Material> materialList = iMaterialService.selectMaterial(materialVO);
        ResponseUtil.showMessage("success", "查询物料成功", materialList);
    }

    /**
     * 根据id更新
     *
     * @param materialVO
     * @throws IOException
     */
    @PutMapping("update")
    public void updateMaterial(@RequestBody MaterialVO materialVO) throws IOException {
        iMaterialService.updateById(materialVO);
        ResponseUtil.showMessage("success", "更新物料成功", "");
    }

    /**
     * 批量删除
     * @param matDbids
     */
    @DeleteMapping("/delete")
    public void delete(@RequestBody String[] matDbids) throws IOException {
        List<String> matDbidList = Arrays.asList(matDbids);
        iMaterialService.removeByIds(matDbidList);
        ResponseUtil.showMessage("success", "删除成功", "");
    }

}
